package com.atguigu.spzx.manager.service.system.impl;

import com.atguigu.spzx.manager.helper.MenuHelper;
import com.atguigu.spzx.manager.mapper.SysMenuMapper;
import com.atguigu.spzx.manager.mapper.SysRoleMenuMapper;
import com.atguigu.spzx.manager.service.system.SysRoleMenuService;
import com.atguigu.spzx.model.dto.system.AssginMenuDto;
import com.atguigu.spzx.model.entity.system.SysMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mahone
 */
@Service
public class SysRoleMenuServiceImpl implements SysRoleMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public Map<String, Object> findSysRoleMenuByRoleId(Long roleId) {
       List<SysMenu> sysMenuList =  sysMenuMapper.selectAllNodes();
        List<SysMenu> sysMenuTree = MenuHelper.buildTree(sysMenuList);
        List<Long> checkedKeys = sysRoleMenuMapper.selectSysRoleMenuByRoleId(roleId);

        Map<String,Object> map = new HashMap<>();
        map.put("sysMenuTreeList",sysMenuTree);
        map.put("checkedKeys",checkedKeys);
        return map;
    }

    @Override
    public void doAssign(AssginMenuDto assginMenuDto) {
        // 删除roleId对应所有菜单
        sysRoleMenuMapper.deleteByRoleId(assginMenuDto.getRoleId());

        // 重新添加菜单
        sysRoleMenuMapper.insertRoleMenu(assginMenuDto);
    }
}
